package com.ysy.tz.mfg.equipment.scale;

import com.ysy.tz.mfg.equipment.CommEquipment;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ToledoScale extends CommEquipment {
    private static final Logger log = LoggerFactory.getLogger(ToledoScale.class);
    int[] dataInput = new int[60];
    int inputIndex = 0;
    int startPoint = 5;
    int endPoint = 10;
    BigDecimal zero = new BigDecimal(0.0);
    BigDecimal ten = new BigDecimal(10.0);
    BigDecimal minWeight = new BigDecimal(5);

    public ToledoScale() {
        this.setDataBits(8);
        this.setStopBits(1);
        this.setParity(0);
    }

    public ToledoScale(String portName, int baudrate, int fractionDigits) {
        super(portName, baudrate, fractionDigits);
        this.setDataBits(8);
        this.setStopBits(1);
        this.setParity(0);
    }

    public BigDecimal getMinWeight() {
        return this.minWeight;
    }

    public void setMinWeight(BigDecimal minWeight) {
        this.minWeight = minWeight;
    }

    public int getStartPoint() {
        return this.startPoint;
    }

    public void setStartPoint(int startPoint) {
        this.startPoint = startPoint;
    }

    public int getEndPoint() {
        return this.endPoint;
    }

    public void setEndPoint(int endPoint) {
        this.endPoint = endPoint;
    }

    protected void messageReceived(InputStream inputStream) throws Exception {
        while(true) {
            try {
                if (inputStream.available() > 0) {
                    int inputInt = inputStream.read();
                    if (inputInt != 13) {
                        this.dataInput[this.inputIndex++] = inputInt;
                        if (this.inputIndex >= 60) {
                            this.inputIndex = 0;
                        }
                        continue;
                    }

                    this.inputIndex = 0;
                    StringBuilder sb = new StringBuilder();

                    for(int i = this.startPoint; i <= this.endPoint; ++i) {
                        sb.append((char)this.dataInput[i]);
                    }

                    try {
                        System.out.println(this.startPoint + "," + this.endPoint + " ---------->>>称数据--->>" + sb.toString().trim());
                        BigDecimal bd = new BigDecimal(sb.toString().trim());

                        for(int i = 0; i < this.getFractionDigits(); ++i) {
                            bd = bd.divide(this.ten, this.getFractionDigits(), 4);
                        }

                        if (bd.compareTo(this.minWeight) > 0) {
                            this.setCurrentWeight(bd);
                        } else {
                            this.setCurrentWeight(this.zero);
                        }
                    } catch (Exception var6) {
                        log.error("toledoScale解析数据失败", var6);
                    }

                    if (this.inputIndex == 60) {
                        this.inputIndex = 0;
                    }
                }
            } catch (IOException var7) {
                log.error("toledoScale读取数据失败", var7);
            }

            return;
        }
    }

    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer();
        sb.append(24);
        System.out.println("---------->>>称数据--->>" + sb.toString());
        BigDecimal bd = new BigDecimal(sb.toString());
        System.out.print(bd);
    }
}
